

package top.continew.admin.question.manger.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.continew.admin.common.context.UserContextHolder;
import top.continew.admin.question.manger.base.PageFactory;
import top.continew.admin.question.dto.QuestionErrorCorrectionDto;
import top.continew.admin.question.dto.QuestionErrorCorrectionQueryDto;
import top.continew.admin.question.mapper.QuestionErrorCorrectionMapper;
import top.continew.admin.question.model.QuestionErrorCorrection;
import top.continew.admin.question.model.question.Question;
import top.continew.admin.question.manger.service.QuestionErrorCorrectionService;
import top.continew.starter.core.validation.ValidationUtils;

import java.util.List;

/**
 * Created with Mac IntelliJ IDEA
 *
 * @Title: QuestionErrorCorrectionServiceImpl
 * @Package: com.yunshi.service.impl
 * @Author: RHY
 * @Description:
 * @Date: 2021/3/26 16:21
 */

@Service
public class QuestionErrorCorrectionServiceImpl extends ServiceImpl<QuestionErrorCorrectionMapper, QuestionErrorCorrection> implements QuestionErrorCorrectionService {

    @Override
    public void addQuestionErrorCorrection(Long userId, Long questionId, String errorContent) {
        Question question = new Question().selectById(questionId);
        QuestionErrorCorrection questionErrorCorrection = new QuestionErrorCorrection();
        questionErrorCorrection.setQuestionId(question.getId());
        questionErrorCorrection.setQuestionSubject(question.getSpecificationType());
        questionErrorCorrection.setErrorContent(errorContent);
        questionErrorCorrection.setHandleStatus(QuestionErrorCorrection.HANDLE_STATUS_NO);
        questionErrorCorrection.setMoneyStatus(QuestionErrorCorrection.MONEY_STATUS_NO);
        questionErrorCorrection.setCreateTime(System.currentTimeMillis());
        this.save(questionErrorCorrection);
    }

    @Override
    public void acceptStatus(Long id) {
        QuestionErrorCorrection questionErrorCorrection = this.getById(id);
        if (questionErrorCorrection.getHandleStatus().equals(QuestionErrorCorrection.HANDLE_STATUS_NO)) {
            questionErrorCorrection.setHandleStatus(QuestionErrorCorrection.HANDLE_STATUS_YES);
            questionErrorCorrection.setHandleTime(System.currentTimeMillis());
            questionErrorCorrection.setHandleUser(UserContextHolder.getUserId());
            this.updateById(questionErrorCorrection);
        } else {
            ValidationUtils.throwIf(true, "纠错已采纳");
        }
    }

    @Override
    public List<QuestionErrorCorrectionDto> pageList(QuestionErrorCorrectionQueryDto queryDto) {
        return PageFactory.page(() -> this.baseMapper.pageList(queryDto));
    }
}
